package k2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Utils {
	/**
	 * 
	 * @param str ( string to get results from ) 
	 * @param open ( start tag )
	 * @param close ( end tag )
	 * @return ( String array containing string(s) between tags )
	 */
	public String[] substringsBetween(String str, String open, String close) {
        if (str == null || open == null || close == null) {
            return null;
        }
        int strLen = str.length();
        if (strLen == 0) {
            return null;
        }
        int closeLen = close.length();
        int openLen = open.length();
        List<String> list = new ArrayList<String>();
        int pos = 0;
        while (pos < (strLen - closeLen)) {
            int start = str.indexOf(open, pos);
            if (start < 0) {
                break;
            }
            start += openLen;
            int end = str.indexOf(close, start);
            if (end < 0) {
                break;
            }
            list.add(str.substring(start, end));
            pos = end + closeLen;
        }
        if (list.isEmpty()) {
            return null;
        } 
        return list.toArray(new String [list.size()]);
    }
	public String properTime(String date){
		String[] buffer = null;
		buffer = date.split("T");
		return buffer[0] + " at " + buffer[1].substring(0, 8);
	}
	public int dateToDays(String date){
		String[] buffer = null;
		String[] toDays = null;
		int days;
		buffer = date.split("T");
		toDays = buffer[0].split("-");
		days = 365*Integer.parseInt(toDays[0]) + 30*Integer.parseInt(toDays[1]) + Integer.parseInt(toDays[2]);
		return days;
	}
	public int currentDateInDays(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return dateToDays(dateFormat.format(date));
	}
}
